<?php
namespace Home\Controller;

use Common\Model\OrderModel;
use Think\Controller;


/**
 * 订单投诉
 * Class MemberComplainController
 * @package Home\Controller
 */
class MemberComplainController extends BaseController
{
    public $obj;
    public $member_id;
    public $nickname;

    public function __construct()
    {
        parent::__construct();

        $this->obj = M('b2c_members');
        $this->member_id = $this->member_data['member_id'];
        $this->nickname = $this->member_data['nickname'];
        if (!$this->member_id) {
            $this->redirect('Login/index');
        }
    }

    /**
     * 退款首页
     */
    public function index()
    {
        $order_id = I('order_id', '', intval());
        if (empty($order_id)) {
            $this->error('失败');
        }
        $order = new OrderModel();
        $orderMsg = $order->getOrderMsg($order_id, $this->member_id); // 查询订单信息
        if (empty($orderMsg)) {
            $this->error('失败');
        }
        $store = M('webpos_local_store')->field('mobile')->where(['local_id' => $orderMsg['local_id']])->find();
        $this->assign([
            'order_id' => $order_id,
            'mobile' => $store['mobile']
        ]);
        $this->display();
    }

    /**
     * ajax退款提交
     */
    public function ajax()
    {
        $order_id = I('order_id', '', intval());
        $message = I('message', '', htmlspecialchars());
        $tel = I('tel', '', htmlspecialchars());
        if (empty($order_id)) {
            echo false;
            exit;
        }
        $order = new OrderModel();
        $orderMsg = $order->getOrderMsg($order_id, $this->member_id); // 查询订单信息
        if (empty($orderMsg)) {
            echo false;
            exit;
        }
        $complain = M('b2c_order_complain');
        $time =date("Y-m-d H:i:s" ,time());
        $data = [
            'tel' => $tel,
            'message' => $message,
            'member_id' => $this->member_id,
            'operation_name' => $this->nickname,
            'order_id' => $order_id,
            'local_id' => $orderMsg['local_id'],
            'time' => $time
        ];

        $complain->add($data);

        echo true;
    }

    /**
     * 列表
     */
    public function lists(){
        //头像
        $user_top_img = session('t_userInfo')['logo'];
        $this->assign('user_top_img',$user_top_img);
        $this->display();
    }

    /**
     * ajax
     */
    public function ajax_lists(){
        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 10);

        $complain = M('b2c_order_complain');
        $count = $complain->where(['member_id' => $this->member_id])
            ->count();
        $complain_msg = $complain->field('id, message, operation_name,time')
            ->where(['member_id' => $this->member_id, 'pid' => 0])
            ->page($page, $num)
            ->order('id desc')
            ->select();
        foreach ($complain_msg as $k=>$v) {
            $complain_msg[$k]['replys'] = $complain->field('id, message, operation_name,time')->where(['pid' => $v['id']])->select();
        }

        $totalPage = ceil($count/$num); // 总页面数

        // 获取电话
        $order_id = I('order_id');
        $order = new OrderModel();
        $orderMsg = $order->getOrderMsg($order_id, $this->member_id); // 查询订单信息
        $store = M('webpos_local_store')->field('mobile')->where(['local_id' => $orderMsg['local_id']])->find();
        echo json_encode([
            'data' => $complain_msg,
            'totalPage' => $totalPage, // 总页数
            'count' => $count, // 总个数
            'page' => $page, // 第几页
            'mobile' => $store['mobile']
        ]);
    }


}